package com.ihrm.company.controller;

import com.ihrm.common.controller.BaseController;
import com.ihrm.common.entity.Result;
import com.ihrm.common.entity.ResultCode;
import com.ihrm.company.service.CompanyService;
import com.ihrm.company.service.DepartmentService;
import com.ihrm.domain.company.Company;
import com.ihrm.domain.company.Department;
import com.ihrm.domain.company.response.DeptListResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @auther chongxiongwei
 * @create 2020-04-15-23:57
 */
@Slf4j
@CrossOrigin  // 解决跨域
@RestController
@RequestMapping("/company/department")
public class DepartmentController extends BaseController {

    @Autowired
    DepartmentService departmentService;

    @Autowired
    CompanyService companyService;

    //保存部门
    @PostMapping("")
    public Result add(@RequestBody Department department){
        // 设置保存部门的所属企业ID
//        String companyId = "1";
        department.setCompanyId(companyId);
        departmentService.save(department);
        return new Result(ResultCode.SUCCESS);
    }

    //更新部门
    @PutMapping("/{id}")
    public Result update(@PathVariable("id") String id,@RequestBody Department department){
        department.setId(id);
        departmentService.update(department);
        return new Result(ResultCode.SUCCESS);

    }

    //删除部门
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") String id){
        departmentService.delete(id);
        return new Result(ResultCode.SUCCESS);
    }

    //根据企业ID查询所有部门
    @GetMapping("")
    public Result findAll(){
//        String companyId = "1";

        log.info("*****companyid******"+companyId);
        Company company = companyService.findById(companyId);
        List<Department> departmentList = departmentService.findAll(companyId);
        // 创建部门list返回对象 需要company ，deptlist
        DeptListResult deptListResult = new DeptListResult(company,departmentList);

        return new Result(ResultCode.SUCCESS,deptListResult);
    }

    //根据部门ID查找部门
    @GetMapping("/{id}")
    public Result findByID(@PathVariable("id") String id){
        Department department = departmentService.findById(id);
        return new Result(ResultCode.SUCCESS,department);
    }

}
